package thunderbirdgtd.tests;

import static org.junit.Assert.*;

import java.awt.Color;
import java.util.LinkedList;
import java.util.ListIterator;

import org.junit.Before;
import org.junit.Test;

import thunderbirdgtd.entities.Action;
import thunderbirdgtd.entities.Project;
import thunderbirdgtd.entities.Tag;


public class ProjectTest {
	
	LinkedList<Project> differentProjects = new LinkedList<Project>();
	LinkedList<Project> sameProjects = new LinkedList<Project>();
	Tag a = new Tag("a");
	Tag b = new Tag("b");
	
	@Before
	public void createProjects()
	{
		Project p1 = new Project("test1");
		Project p2 = new Project("test2");
		
		try {
			p1.addTag(a);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			p2.addTag(b);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		differentProjects.add(p1);
		differentProjects.add(p2);
		
		sameProjects.add(p1);
		
		
	}
	
	@Test
	public void testNoProjectsInATag() {
		try {
			assertTrue((Project.findProjectByTag(sameProjects, b)).isEmpty());
			
		} catch (Exception e) {
			//all fine
		}
	}
	
	public void testSomeProjectsInATag() {
		try {
			assertEquals((Project.findProjectByTag(differentProjects, a)).size()==1,true);
		} catch (Exception e) {
			//all fine
		}
	}
	
	public void testAllProjectsInATag() {
		try {
			assertEquals((Project.findProjectByTag(sameProjects, a)).size()==1,true);
		} catch (Exception e) {
			//all fine
		}
	}

	@Test
	public void testProjectName() {
		Project project = new Project("name");
		
		assertTrue("name".equals(project.getName()));
	}
	
	@Test
	public void testAddAction(){
		Project project = new Project("name");
		
			Action actionTest = new Action("Test");
		
		try {
			project.addAction(actionTest);
			Action returnedAction = Action.searchActionInLinkedList("Test", project.assignedActions);
			assertEquals("Test", returnedAction.getName());
		} catch (Exception e) {
			//fail
		}
	}
	
	@Test
	public void testeGetAction() {
		Project project = new Project("name");
		Action r;
		Action s;
		
		Action actionTest1 = new Action("Test1");
		Action actionTest2 = new Action("Test2");
		Action actionTest3 = new Action("Test3");
		
		LinkedList<Action> list_action = new LinkedList<Action>();
		
		list_action.add(actionTest1);
		list_action.add(actionTest2);
		list_action.add(actionTest3);
		
		try {
			project.addAction(actionTest1);
			project.addAction(actionTest2);
			project.addAction(actionTest3);
			
			LinkedList<Action> returnedActions = project.getActions();
			
			ListIterator<Action> li_send = list_action.listIterator();
			
			ListIterator<Action> li_returned =  returnedActions.listIterator();
			
			assertEquals(list_action.size(), returnedActions.size());
			
			while(li_send.hasNext()){
				r = li_returned.next();
				s = li_send.next();
				
					assertEquals(s.getName(), r.getName());	
			}
		}
		catch (Exception e) {
			//fail
		}
	}
	
	@Test
	public void testHasSubProjects() {
		Project project1 = new Project("project1");
		Project project2 = new Project("project2");
		
		project1.assignSubProject(project2);
		project2.setParentProject(project1);
		
		assertTrue(project1.hasSubProjects());
		assertFalse(project2.hasSubProjects());
	}
	
	@Test
	public void testIsSubProject() {
		Project project1 = new Project("project1");
		Project project2 = new Project("project2");
		
		project1.assignSubProject(project2);
		project2.setParentProject(project1);
		
		assertFalse(project1.isSubProject());
		assertTrue(project2.isSubProject());
	}
}
